package cn.yuanqiao.web.controller.system;

import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.domain.TenantOperLog;
import cn.yuanqiao.system.service.ITenantOperLogService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/tenantOperlog")
public class TenantOperlogController extends BaseController
{
    @Autowired
    private ITenantOperLogService operLogService;

    //@RequiresPermissions("system:operlog:list")
    @GetMapping("/list")
    public TableDataInfo list(TenantOperLog operLog)
    {
        List<TenantOperLog> list = null;
        if ("sysauditor".equals(SecurityUtils.getUsername())){
            startPage();
            list = operLogService.selectOperLogListBySysauditor(operLog);
        } else if ("syssso".equals(SecurityUtils.getUsername())){
            startPage();
            list = operLogService.selectOperLogListBySyssso(operLog);
        }else {
            startPage();
            list = operLogService.selectOperLogList(operLog);
        }
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    //@RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantOperLog operLog)
    {
        List<TenantOperLog> list = null;
        if ("sysauditor".equals(SecurityUtils.getUsername())){
            list = operLogService.selectOperLogListBySysauditor(operLog);
        } else if ("syssso".equals(SecurityUtils.getUsername())){
            list = operLogService.selectOperLogListBySyssso(operLog);
        } else  {
            list = operLogService.selectOperLogList(operLog);
        }
        ExcelUtil<TenantOperLog> util = new ExcelUtil<TenantOperLog>(TenantOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    //@RequiresPermissions("system:operlog:remove")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    //@RequiresPermissions("system:operlog:remove")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        operLogService.cleanOperLog();
        return success();
    }

    @PostMapping
    public AjaxResult add(@RequestBody TenantOperLog operLog)
    {
        return toAjax(operLogService.insertOperlog(operLog));
    }

    @Log(title = "操作日志统计", businessType = BusinessType.SELECT)
    @GetMapping("/operLogTongji")
    public AjaxResult operLogTongji(TenantOperLog operLog)
    {
        List<Long[]> list = null;
        if ("sysauditor".equals(SecurityUtils.getUsername())){
            list = operLogService.operLogTongjiBySysauditor(operLog);
        } else if ("syssso".equals(SecurityUtils.getUsername())){
            list = operLogService.operLogTongjiBySyssso(operLog);
        }else {
            list = operLogService.operLogTongji(operLog);
        }
        return AjaxResult.success(list);
    }

    @Log(title = "操作日志审计", businessType = BusinessType.UPDATE)
    @PostMapping("/upAudit")
    public AjaxResult upAudit(@RequestBody JSONObject jsonObject)
    {
     //   if ("sysauditor".equals(SecurityUtils.getUsername())){
            operLogService.updatAuditByIds(jsonObject);
     //   }
        return success();
    }
}
